﻿using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using Models;
using GDGeek;
using NormalBattleIoc;

public class PlayerInBattle : MonoBehaviour, IComparable
{
    public const string BaseWait = "初始等待状态";
    public const string WaitPlayer_SelectPersonOperation = "等待玩家选择-人物状态要执行哪种操作";
    public const string WaitPlayer_SelectPersonAttackTarget = "角色选择了攻击状态，等待选择攻击目标";
    public const string System_DoAction = "系统自动轮询执行策略状态";


    [SerializeField]
    private Animator animator;
    
    [Header("名字，生命，等级等")]
   // public PersonBase pbase;

    public int NowHp;
    public int MaxHp;
    public int Attack;
    public int FangWei;
    public int Speed;

   // [Header("攻击力防御力等")]
   // public PersonPoint ppoint;
   // [Header("速度，强度，驾驶等等")]
   // public ResistanceAbility pability;
    [Header("人物选择策略面板")]
    public GameObject person_operation_panel;
    [Header("人物选择攻击后，选择共计目标面板")]
    public GameObject person_attack_select_target_panel;

    public string fire_Name;

    public FSM fsm_player = new FSM();

    private string stage_player;
    //选择 攻击状态
    //选择 攻击目标状态
    //系统自动攻击显示状态
    public int next_turn_speed;

    private bool dead = false;
    //经验
    private float currentExperience;

    public Vector3 target_enemy_pos;

    private TurnBattleEventView eventview;
    [Header("角色是否驾驶战车")]
    public bool isDrive = false;
    [Header("驾车图片")]
    public Sprite tank_sprite;
    public Sprite person_sprite;
    private SpriteRenderer player_renderer;
    public void CheckPlayerIsDrive()
    {
        isDrive = GameDataManager.Instance.CheckPersonIsDriveTank();
        if (isDrive)
        {
            player_renderer.sprite = tank_sprite;
        }
        else
        {
            player_renderer.sprite = person_sprite ;
        }
    }

    

    void Start()
    {
        GameObject g = GameObject.Find("TurnBattleEventView");

        eventview = g.GetComponent<TurnBattleEventView>();

        HUDRoot = GameObject.Find("HUDText").GetComponent<bl_HUDText>();

        player_renderer = GetComponent<SpriteRenderer>();

        CheckPlayerIsDrive();
    }

    

    //暂时没有使用
    void InitFSM()
    {
        fsm_player.addState(WaitPlayer_SelectPersonOperation, OnWaitPlayer_SelectPersonOperation());
        fsm_player.addState(WaitPlayer_SelectPersonAttackTarget, OnWaitPlayer_SelectPersonAttackTarget());
        fsm_player.addState(System_DoAction, OnSystem_DoActionState());
        fsm_player.addState(BaseWait, OnBaseWaitState());

        fsm_player.init(BaseWait);
    }

    private State OnBaseWaitState()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_player = state.name;

            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

        //state.addAction("转换入角色普通攻击状态", "普通攻击");

        return state;
    }

    private State OnWaitPlayer_SelectPersonOperation()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_player = state.name;
            //开启 角色操作面板
            person_operation_panel.SetActive(true);
            //通过游戏对象，GetCompent获得组件，， 对它的点击事件 进行 绑定 当前gameobject

            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

        //state.addAction("转换入角色普通攻击状态", "普通攻击");

        return state;
    }


    private State OnWaitPlayer_SelectPersonAttackTarget()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_player = state.name;

            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

        //state.addAction("转换入角色普通攻击状态", "普通攻击");

        return state;
    }
    private State OnSystem_DoActionState()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_player = state.name;

            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

        //state.addAction("转换入角色普通攻击状态", "普通攻击");

        return state;
    }

    public bl_HUDText HUDRoot;


    /// <summary>
    /// 受到攻击伤害
    /// </summary>
    /// <param name="damage"></param>
	public void receiveDamage(int damage)
    {

        this.NowHp  -= damage;
        //  animator.Play("Hit");


        HUDRoot.NewText("- " + damage.ToString(), base.transform, Color.red, 20, 20f, -1f, 2.2f, bl_Guidance.Up);

        if ( this.NowHp <= 0)
        {
            //设置死亡
            this.dead = true;

            HUDRoot.NewText("- " + damage.ToString() + " 玩家阵亡！", base.transform, Color.red, 20, 20f, -1f, 2.2f, bl_Guidance.Up);


          
           // this.gameObject.tag = "DeadUnit";
           // Destroy(this.gameObject);
        }

        eventview.UpdatePlayerHp();
    }

    public void calculateNextActTurn(int currentTurn)
    {
        //Math.Ceiling 向上取整数
        this.next_turn_speed = currentTurn + (int)Math.Ceiling(100.0f / this.Speed);
    }

    public int CompareTo(object otherPlayerInBattle)
    {
        return next_turn_speed.CompareTo(((PlayerInBattle)otherPlayerInBattle).next_turn_speed);
    }

    public bool isDead()
    {
        return this.dead;
    }

    /// <summary>
    /// 给玩家 增加经验值
    /// </summary>
    /// <param name="experience"></param>
	public void receiveExperience(float experience)
    {
        this.currentExperience += experience;
    }
}
